package com.kong.meirimall.product.app;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

//import org.apache.shiro.authz.annotation.RequiresPermissions;
import com.kong.meirimall.product.entity.AttrEntity;
import com.kong.meirimall.product.service.AttrAttrgroupRelationService;
import com.kong.meirimall.product.service.AttrService;
import com.kong.meirimall.product.service.CategoryService;
import com.kong.meirimall.product.vo.AttrGroupRelationVo;
import com.kong.meirimall.product.vo.AttrGroupWithAttrsVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.kong.meirimall.product.entity.AttrGroupEntity;
import com.kong.meirimall.product.service.AttrGroupService;
import com.kong.common.utils.PageUtils;
import com.kong.common.utils.R;



/**
 * 属性分组
 *
 * @author kong
 * @email kong@gmail.com
 * @date 2024-10-03 18:13:07
 */
@RestController
@RequestMapping("product/attrgroup")
public class AttrGroupController {
    @Autowired
    private AttrGroupService attrGroupService;
    @Autowired
    private CategoryService categoryService;
    @Autowired
    AttrService attrService;
    @Autowired
    AttrAttrgroupRelationService attrAttrgroupRelationService;


    //根据 catalogId 获得所有属性分组，并找到每个分组下的 所有属性
    //（要将分组和属性一起返回，所以要新建 Vo，其中属性用List 存放）
    ///product/attrgroup/225/withattr
    @GetMapping("/{catalogId}/withattr")
    public R getAttrGroupWithAttrs(@PathVariable("catalogId") Long catalogId) {
        List<AttrGroupWithAttrsVo> vos=attrGroupService.getAttrGroupWithAttrsBycatalogId(catalogId);
        return R.ok().put("data", vos);
    }
    //新增关联关系
    ///product/attrgroup/attr/relation
    //{attrId: 8, attrGroupId: 8} 接收到数组就用 @RequestBody，用 List<> 没什么区别
    @PostMapping("/attr/relation")
    public R addRelation(@RequestBody List<AttrGroupRelationVo> vos){
        attrAttrgroupRelationService.saveBatch(vos);
        return R.ok();
    }

    //查询分组未关联的属性，返回分页对象
    ///product/attrgroup/{attrgroupId}/noattr/relation
    @GetMapping("/{attrgroupId}/noattr/relation")
    public R attrNoRelation(@PathVariable("attrgroupId") Long attrgroupId,
                            @RequestParam Map<String, Object> params){ //params 是页面分页的参数
        PageUtils page=attrService.getNoRelationAttr(params,attrgroupId);
        return R.ok().put("page",page);
    }

    //查询分组关联的属性,返回对象列表
    ///product/attrgroup/{attrgroupId}/attr/relation
    @GetMapping("/{attrgroupId}/attr/relation")
    public R attrRelation(@PathVariable("attrgroupId") Long attrgroupId){
        List<AttrEntity> entities=attrService.getRelationAttr(attrgroupId);
        return R.ok().put("data",entities);
    }

    // 移除关联属性,要放在 AttrgroupController 中，因为前置路径
    ///product/attrgroup/attr/relation/delete
    @PostMapping("/attr/relation/delete")
    public R deleteRelation(@RequestBody AttrGroupRelationVo[] vos){
        attrService.deleteRelation(vos);
        return R.ok();
    }
    /**
     * 列表
     */
    @RequestMapping("/list/{catalogId}")
    //@RequiresPermissions("product:attrgroup:list")
    public R list(@RequestParam Map<String, Object> params,@PathVariable("catalogId") Long catalogId){
        PageUtils pageUtils = attrGroupService.queryPage(params, catalogId);
        return R.ok().put("page", pageUtils);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{attrGroupId}")
    //@RequiresPermissions("product:attrgroup:info")
    public R info(@PathVariable("attrGroupId") Long attrGroupId){
		AttrGroupEntity attrGroup = attrGroupService.getById(attrGroupId);
        Long catalogId = attrGroup.getCatalogId();
        //查三级分类的完整路径，就用 三级分类的 service CategoryService
        Long[] catelogPath=categoryService.findCatelogPath(catalogId);
        attrGroup.setCatelogPath(catelogPath);
        return R.ok().put("attrGroup", attrGroup);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("product:attrgroup:save")
    public R save(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.save(attrGroup);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("product:attrgroup:update")
    public R update(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.updateById(attrGroup);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("product:attrgroup:delete")
    public R delete(@RequestBody Long[] attrGroupIds){
		attrGroupService.removeByIds(Arrays.asList(attrGroupIds));

        return R.ok();
    }

}
